package se.ramfelt.psnfriends.model.web;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import se.ramfelt.psnfriends.model.Friend;
import se.ramfelt.psnfriends.model.Trophies;

public class FriendListXmlParser extends DefaultHandler {
    private static final String BRONZE = "bronze";
    private static final String SILVER = "silver";
    private static final String GOLD = "gold";
    private static final String PLATINUM = "platinum";
    private static final String LEVEL = "level";
    private static final String TROPHY = "trophy";
    private static final String COMMENT = "comment";
    private static final String AVATAR = "current_avatar";
    private static final String ONLINEID = "onlineid";
    private static final String PSN_FRIEND = "psn_friend";
    private static final String PRESENCE = "current_presence";
    private static final String GAME = "current_game";
    private List<Friend> friends;
    private Friend currentFriend;
    private Trophies currentTrophies;
    private StringBuilder builder;
    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        super.endElement(uri, localName, name);
        if (this.currentFriend != null){
            if (localName.equalsIgnoreCase(ONLINEID)){
                currentFriend.setOnlineId(builder.toString().trim());
            } else if (localName.equalsIgnoreCase(AVATAR)){
                currentFriend.setAvatarUrl(builder.toString().trim());
            } else if (localName.equalsIgnoreCase(COMMENT)){
                currentFriend.setComment(builder.toString().trim());
            } else if (localName.equalsIgnoreCase(PRESENCE)){
                currentFriend.setPresence(builder.toString().trim());
            } else if (localName.equalsIgnoreCase(GAME)){
                currentFriend.setCurrentGame(builder.toString().trim());
            } else if (localName.equalsIgnoreCase(PSN_FRIEND)){
                friends.add(currentFriend);
            } else if (localName.equalsIgnoreCase(TROPHY)){
                currentFriend.setTrophies(currentTrophies);
            } else if (localName.equalsIgnoreCase(LEVEL)){
                currentTrophies.setLevel(Integer.parseInt(builder.toString().trim()));
            } else if (localName.equalsIgnoreCase(PLATINUM)){
                currentTrophies.setPlatinum(Integer.parseInt(builder.toString().trim()));
            } else if (localName.equalsIgnoreCase(GOLD)){
                currentTrophies.setGold(Integer.parseInt(builder.toString().trim()));
            } else if (localName.equalsIgnoreCase(SILVER)){
                currentTrophies.setSilver(Integer.parseInt(builder.toString().trim()));
            } else if (localName.equalsIgnoreCase(BRONZE)){
                currentTrophies.setBronze(Integer.parseInt(builder.toString().trim()));
            }
            builder.setLength(0);    
        }
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        friends = new ArrayList<Friend>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (localName.equalsIgnoreCase(PSN_FRIEND)){
            currentFriend = new Friend();
        }
        if (localName.equalsIgnoreCase(TROPHY)){
            currentTrophies = new Trophies();
        }
    }    
    
    public List<Friend> parse(Reader reader) throws IOException, SAXException, ParserConfigurationException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse(new InputSource(reader), this);
        return friends;		
    }
}
